package com.ibm.csr.staf;

import java.util.ArrayList;
import java.util.List;

import com.ibm.staf.STAFResult;

public class RunThread {

	private EnvManager env;
	private RunConfigure runCon;

	public RunThread(RunConfigure conf) {
		this.runCon = conf;
	}

	/**
	 * KickOff testcase list;
	 * 
	 * @param list
	 * @throws Exception
	 */
	public void StartKickOff(List<Case> list) throws Exception {
		env = new EnvManager();
		TCKickOff.RegisterSTAF();
		// 1. Create a TCKickOff instance with client name only
		// TCKickOff tck = new TCKickOff(runCon.getClientName());
		// 2. Create a TCKickOff instance with client name and workdir
		TCKickOff tck = new TCKickOff(runCon.getClientName(), env
				.get("WORK_DIR"));
		for (Case tc : list) {
			STAFResult sr = null;
			if (tc.isTestcase()) {
				sr = tck.executeCommand(generateCommand((CSTestCase) tc));
			} else {
				sr = tck.executeCommand(generateCommand((CSBucket) tc));
			}
			KickResult kr = new KickResult(sr);
			MSGLog.log(kr.toString());
			if (kr.isSuccess()) {
				tc.setStatus(0);
			} else {
				tc.setStatus(kr.rc);
			}
		}
		TCKickOff.unRegisterSTAF();
	}

	/**
	 * Generate java command for testcase.
	 * 
	 * @param tc
	 * @return
	 */
	public String generateCommand(Case tc) {

		// CSTestCase tc = new CSTestCase();
		// if(c.isTestcase()){
		// tc = (CSTestCase)c;
		// }

		// If the testcase has special command.
		if (tc.getPrivateCommand() != null
				&& !tc.getPrivateCommand().equals("")) {
			return tc.getPrivateCommand().replace("<SERVER>",
					runCon.getServerName()).replace("<TRACELEVEL>",
					runCon.getTracelevel());
		}

		String commandString = "";
		String javaCommand = "";
		String libHome = "";
		String newHarness = "";
		String classPath = "";
		String jccVersion = "";
		String jccHarness = "com.ibm.db2.jcc.harness.JccHarness";
		if (tc == null) {
			return "";
		}

		// Get value from staf-env.properties
		libHome = env.get("JCC_LIB");
		newHarness = env.get("NewJccHarness");
		// classPath = env.get("CLASSPATH");

		if (tc.getJavaVersion() != null) {
			javaCommand = env.get(tc.getJavaVersion());
		} else {
			javaCommand = env.get("DefaultJava");
		}

		// Check the parameters
		if (javaCommand == null || javaCommand.equals("")) {
			javaCommand = "C:/Java60/bin/java";
		}
		if (libHome == null || libHome.equals("")) {
			libHome = "C:/jcc_home/jcc/lib/";
		}
		if (newHarness == null || newHarness.equals("")) {
			newHarness = "C:/newHarness/";
		}
		if (classPath == null || classPath.equals("")) {
			classPath = ".;";
		}

		if (tc.isTestcase()) {
			CSTestCase ctc = (CSTestCase) tc;
			// Check the jcc version
			if (tc.getJccVersion() != null && !tc.getJccVersion().equals("")) {
				jccVersion = tc.getJccVersion();
			} else if (ctc.getOwnBucket() != null
					&& ctc.getOwnBucket().getJccVersion() != null
					&& !ctc.getOwnBucket().getJccVersion().equals("")) {
				// If the testcase have not special jcc requirement, use the
				// bucket
				// jcc version.
				jccVersion = ctc.getOwnBucket().getJccVersion();
			} else {
				// Else use the default jcc version from staf-env.properties
				jccVersion = env.get("JccVersion");
			}
		} else {
			if (tc.getJccVersion() != null && !tc.getJccVersion().equals("")) {
				// If the testcase have not special jcc requirement, use the
				// bucket
				// jcc version.
				jccVersion = tc.getJccVersion();
			} else {
				// Else use the default jcc version from staf-env.properties
				jccVersion = env.get("JccVersion");
			}
		}

		// Jcc lib directory
		libHome = libHome + "/" + jccVersion + "/";

		// Generate jcc lib path
		if (jccVersion.startsWith("3")) {
			// If it's jcc 3.x.x, use db2jcc.jar, sqlj.zip
			classPath = env.get("commonClassPath_new");
			classPath = classPath.replaceAll("jccver", jccVersion);
			// classPath = libHome + "db2jcc.jar;" + libHome
			// + "db2jccharness.jar;" + libHome + "sqlj.zip;" + libHome
			// + "db2jcc_license_cisuz.jar;" + classPath;
		} else if (jccVersion.startsWith("4")) {
			// If it's jcc 4.x.x, use db2jcc4.jar, sqlj4.zip
			classPath = env.get("commonClassPathFor4_1AndLater_new");
			classPath = classPath.replaceAll("jccver", jccVersion);
			// classPath = libHome + "db2jcc4.jar;" + libHome
			// + "db2jccharness.jar;" + libHome + "sqlj4.zip;" + libHome
			// + "db2jcc_license_cisuz.jar;" + classPath;
		}

		// If the testcase need new version JccHarness, add newHarness path to
		// the classpath.
		if (tc.isNewHarness()) {
			classPath = newHarness + ";" + classPath;
		}

		// Generate java command.
		if (tc.isTestcase()) {
			commandString += javaCommand + " -cp " + classPath + " "
					+ jccHarness + runCon.generateParam() + " -test "
					+ tc.getCaseName();
		} else {
			commandString += javaCommand + " -cp " + classPath + " "
					+ jccHarness + runCon.generateParam() + " -bucket "
					+ tc.getCaseName();
		}
		return commandString;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		RunConfigure conf = new RunConfigure();
		conf.setClientName("db2csw03.svl.ibm.com");
		conf.setServerName("csrec814.vmec.svl.ibm.com");
		RunThread rt = new RunThread(conf);
		List<Case> tcs = new ArrayList<Case>();
		CSTestCase tc = new CSTestCase();
		tc
				.setCaseName("com.ibm.db2.jcc.harness.fvt.zos.v9.dk257xmltable_j02.Dk257022");
		tc.setNewHarness(true);
		tc.setJccVersion("4.11.10");
		tcs.add(tc);
		try {
			rt.StartKickOff(tcs);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
